/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.serverpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.L1Party;
import l1j.server.server.model.Instance.L1PcInstance;

public class S_Party extends ServerBasePacket {
	private static final String _S_Party = "[S] S_Party";
	private byte[] _byte = null;
	private static final Logger _log = Logger.getLogger(S_Party.class.getName());

	public S_Party(final int type, final L1PcInstance pc) {// TODO 3.3C 組隊系統
		switch (type) {
		case 105:
			this.oldMember(pc);
			break;
		case 106:
			this.changeLeader(pc.getId() , pc.getName());
			break;
		default:
			break;
		}
	}
	/**隊伍成員小地圖座標更新*/
	public S_Party(final L1Party party , final L1PcInstance pc){
		this.refreshParty(party , pc);
	}
	/**新加入隊伍的玩家*/
	public S_Party(final L1PcInstance pc , final L1Party party){
		this.newMember(pc, party);
	}
	/**隊伍成員資料(隊長)*/
	public S_Party(final L1PcInstance pc){
		this.oldMember(pc);
	}

	public S_Party(final String htmlid, final int objid) {
		this.buildPacket(htmlid, objid, "", "", 0);
	}

	public S_Party(final String htmlid, final int objid,final String partyname, final String partymembers) {
		this.buildPacket(htmlid, objid, partyname, partymembers, 1);
	}

	private void buildPacket(final String htmlid, final int objid,
			final String partyname, final String partymembers, final int type) {
		this.writeC(S_OPCODE_SHOWHTML);
		this.writeD(objid);
		this.writeS(htmlid);
		this.writeH(type);
		this.writeH(0x02);
		this.writeS(partyname);
		this.writeS(partymembers);
	}

	/**
	 * 更換隊長
	 * 
	 * @param pc
	 */
	public void changeLeader(final int objid , final String name) {
		this.writeC(S_OPCODE_PACKETBOX);
		this.writeC(S_PacketBox.PATRY_SET_MASTER);
		this.writeD(objid);
		this.writeS(name);
		//	this.writeH(0x0000);
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		}

		return this._byte;
	}

	@Override
	public String getType() {
		return _S_Party;
	}

	/**
	 * 新加入隊伍的玩家
	 * 
	 * @param pc
	 */
	private void newMember(final L1PcInstance pc , final L1Party party) {
		try {
//			final L1PcInstance leader = pc.getParty().getLeader();
			if (pc.getParty() == null) {
				return;
			} else {
				final L1PcInstance member[] = pc.getParty().getMembers();
				this.writeC(S_OPCODE_PACKETBOX);
				this.writeC(S_PacketBox.UPDATE_OLD_PART_MEMBER);
				this.writeC(member.length - 1);// 忽略自己資訊 因此-1
				
//				nowhp = leader.getCurrentHp();
//				maxhp = leader.getMaxHp();
//				this.writeD(leader.getId());
//				this.writeS(leader.getName());
//				this.writeC((int) (nowhp / maxhp) * 100);
//				this.writeD(leader.getMapId());
//				this.writeH(leader.getX());
//				this.writeH(leader.getY());
				for (final L1PcInstance element : member) {
					if (element.equals(pc) || element == null) {
						continue;
					}
					double nowhp = element.getCurrentHp();
					double maxhp = element.getMaxHp();
					this.writeD(element.getId());
					this.writeS(element.getName());
					this.writeC((int) (nowhp / maxhp) * 100);
					this.writeD(element.getMapId());
					this.writeH(element.getX());
					this.writeH(element.getY());
				}
//				this.writeC(0x00);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}

	/**
	 * 隊伍成員資料(隊長)
	 * 
	 * @param pc
	 */
	private void oldMember(final L1PcInstance pc) {
		try {
		this.writeC(S_OPCODE_PACKETBOX);
		this.writeC(S_PacketBox.PATRY_UPDATE_MEMBER);
		this.writeD(pc.getId());
		this.writeS(pc.getName());
		final double nowhp = pc.getCurrentHp();
		final double maxhp = pc.getMaxHp();
		this.writeC((int) ((nowhp / maxhp) * 100D));
		this.writeD(pc.getMapId());
		this.writeH(pc.getX());
		this.writeH(pc.getY());
		}catch(Exception e){
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	//	/**
	//	 * 更新隊伍
	//	 * 
	//	 * @param pc
	//	 */
	//	public void refreshParty(final L1PcInstance pc) {
	//		final L1Party party = pc.getParty();
	//
	//		if (party == null) {
	//			return;
	//		} else {
	//			final L1PcInstance member[] = party.getMembers();
	//			this.writeC(S_OPCODE_PACKETBOX);
	//			this.writeC(S_PacketBox.PATRY_MEMBERS);
	//			this.writeC(member.length);
	//			for (final L1PcInstance element : member) {
	//				if (element.equals(pc)) {// 忽略自己
	//					continue;
	//				}
	//				this.writeD(element.getId());
	//				this.writeD(element.getMapId());
	//				this.writeH(element.getX());
	//				this.writeH(element.getY());
	//			}
	//			this.writeC(0x00);
	//		}
	//	}


	/**
	 * 更新隊伍
	 * 
	 * @param pc
	 */
	private void refreshParty(final L1Party party , final L1PcInstance pc) {
		try {
			final L1PcInstance[] member = party.getMembers();
				writeC(S_OPCODE_PACKETBOX);
				writeC(S_PacketBox.PATRY_MEMBERS);
				writeC(member.length-1);
				for (int i = 0, a = member.length; i < a; i++) {
					if (member[i] == null) {
						continue;
					}
					if (pc.equals(member[i])) {// 忽略自己
						continue;
					}
					writeD(member[i].getId());
					writeD(member[i].getMapId());
					writeH(member[i].getX());
					writeH(member[i].getY());
				}
							writeC(0xff);
				//			writeC(0xff);
			
		}catch(Exception e){
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}
}
